﻿using System.Linq;
using System.Windows.Input;
using ExtensionMethods;

namespace RadioPlayer
{
    using System;
    using System.Windows;
    using System.Windows.Controls;
    using MahApps.Metro.Controls;

    using WMPLib;

    /// <summary>
    /// Interaktionslogik für MainWindow.xaml
    /// </summary>
    public partial class MainWindow : MetroWindow
    {
        #region Fields

        private RPlayer _player;

        #endregion Fields

        #region Constructors

        public MainWindow()
        {
            
            Player = new RPlayer();
            InitializeComponent();
            Player.Wmp.PlayStateChange += PlayerPlayStateChanged;
            Player.Wmp.MediaError += PlayerMediaError;
            VolumeSlider.DataContext = Player.Wmp; 
            PlayButtonImage.Source = Player.SetPlayButtonImage(false);
            MuteButtonImage.Source = Player.SetMuteButtonImage(false);
            ThumbPlayStop.ImageSource = Player.SetPlayButtonImage(false);
            ThumbMuteUnmute.ImageSource = Player.SetMuteButtonImage(false);
        }

        #endregion Constructors

        #region Properties

        public RPlayer Player
        {
            get { return _player; }
            set { _player = value; }
        }

        
        #endregion Properties

        #region Methods

        

        private void Button_Click(object sender, RoutedEventArgs e)
        {
            bool? result = new DialogWindow(Player).ShowDialog();
            if(result != null && (bool) result){ Player.WriteStreamsToJson();}
        }

        private void Button_Click_1(object sender, RoutedEventArgs e)
        {
            string warning = "Are you sure you want to delete " + Player.RadioStreams[StationComboBox.SelectedIndex].StreamName + "?";
               MessageBoxResult resut = MessageBox.Show(warning, "ARE YOU SURE?", MessageBoxButton.YesNo, MessageBoxImage.Exclamation,
                            MessageBoxResult.No);
            if (resut == MessageBoxResult.Yes)
            {
                if(Player.IsPlaying && Player.RadioStreams[StationComboBox.SelectedIndex].StreamUri == Player.SelectedStationUri)
                {
                    Player.Wmp.controls.stop();
                }

                Player.RadioStreams.RemoveAt(StationComboBox.SelectedIndex);
                Player.WriteStreamsToJson();
                StationComboBox.Items.Refresh();

            }
        }

        private void MetroWindow_Closing(object sender, System.ComponentModel.CancelEventArgs e)
        {
            Player.WriteStreamsToSettings();
            Player.WriteStreamsToJson();
        }

        private void MuteButtonClick(object sender, RoutedEventArgs e)
        {
            
        }
        

        private void PlayerMediaError(object pMediaObject)
        {
            lblstatus.Content = "error.";
        }

        private void PlayerPlayStateChanged(int newState)
        {
            switch ((WMPPlayState)newState)
            {
                case WMPPlayState.wmppsStopped:
                    _player.IsPlaying = false;
                    lblstatus.Content = "offline."; PlayButtonImage.Source = Player.SetPlayButtonImage(false);
                    ThumbPlayStop.ImageSource = Player.SetPlayButtonImage(false);
                    break;

                case WMPPlayState.wmppsWaiting:
                    lblstatus.Content = "waiting .. "; break;

                case WMPPlayState.wmppsBuffering:
                    lblstatus.Content = "loading ..";
                    break;

                case WMPPlayState.wmppsPlaying:
                    _player.IsPlaying = true;
                    lblstatus.Content = "playing."; PlayButtonImage.Source = Player.SetPlayButtonImage(true);
                    ThumbPlayStop.ImageSource = Player.SetPlayButtonImage(true);
                    Player.RadioStreams[StationComboBox.SelectedIndex].LastPlayed = DateTime.Now;
                    break;

            }
        }

        private void StationComboBoxSelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            try
            {
                Player.SelectedStationName = Player.RadioStreams[StationComboBox.SelectedIndex].StreamName;
                Player.SelectedStationUri = Player.RadioStreams[StationComboBox.SelectedIndex].StreamUri;
                Player.WriteStreamsToSettings();
                if (Player.IsPlaying)
                {
                    Player.Wmp.URL = Player.SelectedStationUri;
                }
            }
            catch (Exception)
            {
                StationComboBox.SelectedIndex = 0;
            }
        }

        private void WindowLoaded(object sender, RoutedEventArgs e)
        {
            var button = MyVisualTreeHelper.FindChild<Button>(this, "btnMax");
            button.Visibility = Visibility.Hidden;

            StationComboBox.ItemsSource = Player.RadioStreams;

            StationComboBox.SelectedIndex = StationComboBox.Items.Count -1;
            //Player.SelectedStationName = Player.RadioStreams[0].StreamName;
            //Player.SelectedStationUri = Player.RadioStreams[0].StreamUri;
        }

        #endregion Methods

        

        #region Commands
        public static RoutedCommand PlayStopCommand = new RoutedCommand();

        private void PlayStopCommandBindingCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }

        private void PlayStopCommandBindingExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            if (Player.IsPlaying)
            {
                if (Player.Wmp != null)
                {
                    Player.Wmp.controls.stop();
                    Player.IsPlaying = false;
                    
                }

            }
            else
            {
                lblstatus.Content = "contacting ..";
                if (_player.SelectedStationUri != null)
                {

                    Player.Wmp.URL = _player.SelectedStationUri;
                    Player.Wmp.controls.play();
                    Player.IsPlaying = true;
                }
            }
        }

        public static RoutedCommand MuteUnmuteCommand = new RoutedCommand();
        private void MuteUnmuteCommandBindingCanExecute(object sender, CanExecuteRoutedEventArgs e)
        {
            e.CanExecute = true;
        }
        private void MuteUnmuteCommandBindingExecuted(object sender, ExecutedRoutedEventArgs e)
        {
            if (Player.Wmp.settings.mute)
            {
                Player.Wmp.settings.mute = false;
                MuteButtonImage.Source = Player.SetMuteButtonImage(false);
                ThumbMuteUnmute.ImageSource = Player.SetMuteButtonImage(false);

            }
            else
            {
                MuteButtonImage.Source = Player.SetMuteButtonImage(true);
                Player.Wmp.settings.mute = true;
                ThumbMuteUnmute.ImageSource = Player.SetMuteButtonImage(true);

            }
        }
        #endregion
    }
}